<template>
    <div class="am-u-sm-12 am-u-md-12 am-u-lg-12">
        <div class="widget am-cf ">
            <div class="widget-head am-cf">
                <div class="widget-title am-fl">
                    <!-- <button type="button" class="am-btn am-btn-default am-btn-success">
                        查看已禁用的退费班级属性
                    </button> -->
                    退费禁用设置
                </div>
                <div class="widget-function am-fr">
                    <button type="button" class="am-btn am-btn-default" @click="$router.go(-1)">返回</button>
                </div>
            </div>
            
            <div class="widget-body  am-fr">
                <div class="see-btn" @click="openTable" >
                    <div class="am-form-group am-btn-group-xs">
                        <button type="button" class="am-btn am-btn-default am-btn-success">
                            查看已禁用的退费班级属性
                        </button>
                    </div>
                </div>
                <ul id="treeDemo" class="ztree"></ul>
                <div class="am-g setting-btn">
                    <div class="am-u-sm-12 am-u-sm-centered " align="center">
                        <button type="button" class="am-btn am-btn-primary" @click="save">保存</button>
                    </div>
                </div>
            </div>
        </div>
        <return-ban ref="ReturnBan" ></return-ban>
    </div>
</template>

<script>
require("../../../static/ztree/zTreeStyle.css");
require("../../../static/ztree/jquery.ztree.all.min.js");
import io from "../../lib/io";
import util from "../../lib/util";
import ReturnBan from "./ReturnBan.vue"

export default {
  data() {
    return {};
  },
  components: {ReturnBan},
  watch: {},
  created() {
    setTimeout(function() {
      var link = document.createElement("link");
      link.setAttribute("rel", "stylesheet");
      link.setAttribute("type", "text/css");
      link.setAttribute("href", "static/ztree/zTreeStyle.css");
      document.getElementsByTagName("head")[0].appendChild(link);
    });
  },
  computed: {},
  mounted() {
    this.queryRefundSettingBusteams();
  },
  methods: {
    openTable(){

        this.$refs.ReturnBan.show()
    },
    queryRefundSettingBusteams() {
      io.post(io.queryRefundSettingBusteams, {}, ret => {
        if (ret.success) {
          let areaTeams = ret.data;
          let zNodes = [];
          for (var i = 0; i < areaTeams.length; i++) {
            let areaTeam = areaTeams[i];
            let periods = areaTeams[i].periodTeamVoList;
            zNodes.push({
              id: areaTeam.areaTeamId,
              name: areaTeam.areamTeamName
            });
            for (var pi = 0; pi < periods.length; pi++) {
              let period = periods[pi];
              let busTeams = periods[pi].busTeamVoList;
              zNodes.push({
                id: areaTeam.areaTeamId + "|" + period.periodId + "|period",
                pId: areaTeam.areaTeamId,
                name: period.periodName
              });
              for (var bi = 0; bi < busTeams.length; bi++) {
                let busTeam = busTeams[bi];
                let baseGrades = busTeams[bi].baseGradeList;
                zNodes.push({
                  id:
                    areaTeam.areaTeamId +
                    "|" +
                    period.periodId +
                    "|" +
                    busTeam.busTaeamId +
                    "|busTeam",
                  pId: areaTeam.areaTeamId + "|" + period.periodId + "|period",
                  name: busTeam.budTeamName
                });
                for (var gi = 0; gi < baseGrades.length; gi++) {
                  let grade = baseGrades[gi];
                  zNodes.push({
                    id:
                      areaTeam.areaTeamId +
                      "&" +
                      areaTeam.areamTeamName +
                      "|" +
                      period.periodId +
                      "&" +
                      period.periodName +
                      "|" +
                      busTeam.busTaeamId +
                      "&" +
                      busTeam.budTeamName +
                      "$" +
                      grade.gradeId +
                      "|gradeNode",
                    pId:
                      areaTeam.areaTeamId +
                      "|" +
                      period.periodId +
                      "|" +
                      busTeam.busTaeamId +
                      "|busTeam",
                    name: grade.gradeName
                  });
                }
              }
            }
          }
          console.log(zNodes);
          this.initZTree(zNodes);
        }
      });
    },
    save() {
      var sNodes = this.$zTree
        .getCheckedNodes()
        .filter(item => item.id.indexOf("gradeNode") > 0);

      if (sNodes.length == 0) {
        this.$alert("请选择禁止退费班级");
        return;
      }
      let arrayData = [];
      let sNodesChange = []
      for (var i = 0; i < sNodes.length; i++) {
        var info = sNodes[i].id;
        sNodesChange.push({
            keys:info.split('$')[0],
            gradeId:info.split('$')[1].split('|')[0],
            gradeName:sNodes[i].name
        })
      }
      console.log(sNodesChange)
     
        for(var i=0;i<sNodesChange.length;i++){
            let num = 0
            let inNew=false
            let newIndex=0
            for(var k=0;k<arrayData.length;k++){
                if(arrayData[k].keys==sNodesChange[i].keys){
                    inNew=true
                    newIndex=k
                    break;
                }
            }
            if(inNew){
                arrayData[newIndex].areaTeamId = sNodesChange[i].keys.split("|")[0].split("&")[0]
                arrayData[newIndex].areaTeamName = sNodesChange[i].keys.split("|")[0].split("&")[1]
                arrayData[newIndex].periodId = sNodesChange[i].keys.split("|")[1].split("&")[0]
                arrayData[newIndex].periodName = sNodesChange[i].keys.split("|")[1].split("&")[1]
                arrayData[newIndex].busTeamId = sNodesChange[i].keys.split("|")[2].split("&")[0]
                arrayData[newIndex].busTeamName = sNodesChange[i].keys.split("|")[2].split("&")[1]
                arrayData[newIndex].gradeIds = arrayData[k].gradeIds+','+sNodesChange[i].gradeId
                arrayData[newIndex].gradeNames = arrayData[k].gradeNames+','+sNodesChange[i].gradeName
            }else{
                arrayData.push({
                    keys:sNodesChange[i].keys,
                    areaTeamId : sNodesChange[i].keys.split("|")[0].split("&")[0],
                    areaTeamName :sNodesChange[i].keys.split("|")[0].split("&")[1],
                    periodId : sNodesChange[i].keys.split("|")[1].split("&")[0],
                    periodName : sNodesChange[i].keys.split("|")[1].split("&")[1],
                    busTeamId : sNodesChange[i].keys.split("|")[2].split("&")[0],
                    busTeamName : sNodesChange[i].keys.split("|")[2].split("&")[1],
                    gradeIds : sNodesChange[i].gradeId,
                    gradeNames : sNodesChange[i].gradeName
                })
            }
        }
        for(var i=0;i<arrayData.length;i++){
            delete arrayData[i].keys
        }
      let param = {
        refundSettings: arrayData
      };
      console.log(param);
      io.postPlayload(
        io.saveOrUpadteRefundSetting,
        param,
        ret => {
          if (ret.success) {
            this.$alert("设置成功");
          } else {
            this.$alert(ret.desc);
          }
        }
      );
    },
    initZTree: function(zNodes) {
      this.$zTree = $.fn.zTree.init(
        $("#treeDemo"),
        {
          check: {
            enable: true,
            chkboxType: { Y: "ps", N: "ps" }
          },
          data: {
            simpleData: {
              enable: true
            }
          }
        },
        zNodes
      );
    }
  }
};
</script>
<style lang="less" scoped>
.widget {
  min-height: 500px;
  .see-btn {
    width: 100%;
    margin-top: 20px;
  }
  .setting-btn{
      margin-top:30px;
  }
}
</style>

